public class MySingleList implements IList{
//    内部类
    public class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    //    链表的属性，头节点
    public ListNode head;

    public void createList(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }

    /*
    * 头插法
    * */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    /*
    * 尾差法
    * */
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
//        链表为空的情况
        if(head == null){
            head = node;
        }else {
            ListNode cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }
/*
* 在index位置插入data
* */
    @Override
    public void addIndex(int index, int data) throws IndexException{
//        index是否合法
        ListNode node = new ListNode(data);
        if (index < 0 || index > size() ){
            throw new IndexException("index不合法："+index);
        }

//        在0位置插入
        if (index == 0){
            addFirst(data);
        }
//        在最后一个位置插入
        if (index == size()){
            addLast(data);
        }
//      中间插入，走到index-1的位置
        ListNode cur = searchPreIndex(index);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode searchPreIndex(int index){
        ListNode cur = head;
        int count = 0;
        while(count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    /*
    * 当前链表是否包含key
    * */
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /*
    * 删除key
    * */
    @Override
    public void remove(int key) {
        if (head == null){
            return;
        }
        if (head.val == key){
            head = head.next;
            return;
        }

        ListNode cur = findPrevKey(key);
        if (cur == null){
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
////        1.找到key
//        ListNode cur = head;
//        ListNode pre = cur.next;
//      可能存在空指针异常
//        while(cur != null){
//            if (cur.val == key){
//                pre.next = cur.next;
//            }else {
//                cur = cur.next;
//                pre = pre.next;
//            }
//        }

    }

    private ListNode findPrevKey(int key){
        ListNode cur = head;
        while(cur.next != null){
            if (cur.next.val == key){
                return cur;
            }else {
                cur = cur.next;
            }
        }
        return null;
    }

    /*
    * 删除所有key
    * */
    @Override
    public void removeAllKey(int key) {
        if (head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode pre = head;

        while(cur != null){
            if (cur.val == key){
                pre.next = cur.next;
                cur = cur.next;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }

        if (head.val == key){
            head = head.next;
        }
    }

    /*
    * 当前链表长度
    * */
    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
